<?php

/**
 * 陈列故障
 * 张名尊， 20140426
 */ 

class faultAction extends backendAction{
    
    public function _initialize() {
        parent::_initialize();
        $this->faultType =  $_REQUEST['faultType']?trim($_REQUEST['faultType']):'modelFault';
        $this->assign('faultType',$this->faultType);
        //$admin = D('admin')->get_user_info(15);
        //setUserSession($admin);
    }    

    public function index(){
        
        if($this->faultType=='modelFault'){
            $addName = '添加机型报障';
        }elseif($this->faultType=='showcaseFault'){
            $addName = '添加体验台报障';
        }
        $big_menu = array( 
                'title' =>$addName,
                'iframe' => U('fault/oppt',array('opttype'=>'add','faultType'=>$this->faultType)),
                'id' => 'add',
                'width' => '500',
                'height' => '400' 
        );
        
        $this->assign('big_menu', $big_menu);        
        $this->readyData();
        $this->list_relation = true;
        
        $map = $this->_search();
        $map['faultType'] = $this->faultType;
        $mod = D($this->_name);
        !empty($mod) && $this->_list($mod, $map);
        
        
        $list = $this->get('list');
        $ids = '';
        foreach($list as $k=>$v){
            $ids .=$v['id'].',';
        } 
        //这是属性的值       
        $attrValue = D('attribute')->getAttributeValue($this->faultType,$ids);
        //这是属性列表
        $attrList = D('attribute')->getAttribute($this->faultType);
        //$attrList = $attrList[''];
        $this->assign('attrValue',$attrValue);
        $this->assign('attrList',$attrList);
        $this->display();        
        
    }
    
    
    /**
     * 审核及查看
     * 
     */ 
    public function verify(){     
        $id = $this->_get('id','intval');
        $info = D('fault')->getDetail($id);
        $info = $info[0];
        $this->assign('info',$info);
        
        $isVerify = $this->_request('isVerify');
        $actionName = '查看详情';
        // 审核来了
        if($isVerify){
            $actionName = '审核';
            //准备结果列表
            $verifyResult = isCanVerify($info['step'],$this->faultType);
            $this->assign('verifyResult',$verifyResult);
            //准备历史记录
            $list = M('verify')->where(array('faultId'=>$id))->select();
            $this->assign('verifyHistoryList',$list);
        }
        $this->assign('isVerify',$isVerify);
        $this->assign('actionName',$actionName);
        $this->display();
    }
    
    /**
     * 审核提交数据处理
     */ 
    public function actVerify(){
       $model = D('verify');
       $data  = $model->create();
       $verifyResult = isCanVerify($_REQUEST['step'],$this->faultType); 
       $data['userId'] = $_SESSION[C('user_session_name')]['id'];
       $data['realName'] = $_SESSION[C('user_session_name')]['realName'];
       $data['verifyTime'] = time();
       $newStep = intval($data['verifyResult']);
       $data['verifyResult'] = $verifyResult[$newStep]['con'];
       if(!$data['verifyResult']){
            $data['verifyResult'] = '';
       }
       if($res = M('fault')->where(array('faultType'=>$this->faultType,'id'=>$data['faultId']))->save(array('step'=>$newStep))){
            $res = $model->add($data);
       }
       if($res){
           $this->success(L('operation_success'),U('fault/index',array('faultType'=>$this->faultType)));
       }else{
           $this->error(L('operation_failure'),U('fault/index',array('faultType'=>$this->faultType)));
       }       
    }
    
    public function oppt(){
        $type = $this->_request('opttype');
        $this->assign('type',$type);
        $this->readyData();
        if(IS_POST){
            // 多图片处理
            if(isset($_FILES['phoot'])){
                $uploadInfo = $this->_upload($_FILES['phoot'],C('fault_attr_dir'));
                if($uploadInfo['error']==0){
                    $arr = array();
                    foreach($uploadInfo['info'] as $v){
                        $arr[] = $v['savename'];
                    }
                    $_POST['photo'] = serialize($arr);    
                }
            }
            if(isset($_POST['regionId'])){
                $arr = explode('_',$_POST['regionId']);
                    if(count($arr)==2){
                        $_POST['regionId'] = $arr[0];
                        $_POST['storeId']  = $arr[1];
                    }
                }
            }
        
        
        $this->$type();              
        $this->display();
    }
    
    protected function _before_insert($data){
        
    }
    
    
    /**
     *需要属性列表  
     * 
     */   
    private function readyData(){
        //品牌列表 
        $brandList = D('brand')->cate_cache();
        $this->assign('brandList',$brandList);
        
        //配制文件
        $faultStatus = C('FUALT_STATUS'); 
        $this->assign('faultStatus',$faultStatus);
        
        //属性列表
        $attrilist = D('attribute')->getAttribute($this->faultType);
        $this->assign('attrilist',$attrilist);   
            
    }
    
    public function getAttribute(){
       $attrilist = D('attribute')->getAttribute($this->faultType);
       $this->ajaxreturn(1,'',$attrilist);        
    
    }
}





?>